package com.tianpengtech.front.web.main;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.tianpengtech.common.model.task.Task;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.common.util.DateUtils;
import com.tianpengtech.front.web.common.constant.Page;
import com.tianpengtech.front.web.common.controller.BaseController;
import com.tianpengtech.front.web.login.LoginService;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller for web users with logged in.
 * @author Delicate
 */
//@Clear(PassportInterceptor.class)
public class MainWebController extends BaseController {

    private MainService mainService=new MainService();

    public void index(){
    	
    	
    	String sql="select * from fair_message order by id desc limit 1";
    	Record record = Db.findFirst(sql);
        if (record!=null){
            record.set("create_time", DateUtils.formateData(record.getDate("create_time"), DateUtils.YYMMDDHHMMSS_FORMAT));
        }
    	setAttr("record",record);
        renderTemplate(Page.MAIN);
    }

    public void welcome() {
    	User user=getAttr(LoginService.loginAccountCacheName);
        setAttr("fairs",mainService.mainFair());
        List<Task> taskList =mainService.mainTask(user.getLong(User.ID));
        List<Task> taskList1=new ArrayList<>();
        List<Task> taskList2=new ArrayList<>();
        for (int i = 0; i < taskList.size(); i++) {
            if (taskList.size()<=3){
                if (i<3){
                    taskList1.add(taskList.get(i));
                }
            }else{
                if (i<3){
                    taskList1.add(taskList.get(i));
                }else{
                    taskList2.add(taskList.get(i));
                }
            }

        }

        setAttr("tasks1",taskList1);
        setAttr("tasks2",taskList2);
        renderTemplate(Page.WELCOME);
    }
}
